package main

import (
	"github.com/gin-gonic/gin"
	"github.com/spf13/viper"
	"itluv.me/ginessential/common"
	"os"
)

func main() {
	InitConfig()
	// 初始化database
	db := common.InitDB()
	// 关闭链接 https://github.com/go-gorm/gorm/issues/3216 https://gorm.io/zh_CN/docs/generic_interface.html
	sqlDB, errs := db.DB()
	defer sqlDB.Close()
	if errs != nil {
		panic("failed to close database, rtt: " + errs.Error())
	}

	// 获取gin顶级对象
	r := gin.Default()
	// r对象挂载路由
	r = CollectRoute(r)

	// 从yml文件读取配置端口
	port := viper.GetString("server.port")
	if port != "" {
		panic(r.Run(":" + port))
	}
	// 运行输出http地址
	panic(r.Run())
}

func InitConfig() {
	workDir, _ := os.Getwd()
	viper.SetConfigName("application")
	viper.SetConfigType("yml")
	viper.AddConfigPath(workDir + "/config")
	err := viper.ReadInConfig()
	if err != nil {
		panic("")
	}
}
